//
//  HuangwwFaceAndGestureTool.m
//  Pods
//
//  Created by huangwenwu on 2020/1/14.
//

#import "HuangwwFaceAndGestureTool.h"
#import "HuangwwAuthID.h"
#import "HuangwwPCCircleViewConst.h"
#import "HuangwwGestureVerifyViewController.h"
#import "HuangwwDrawGestureViewController.h"
#import <FuliBaseLibraryFramework/FuliBaseLibraryFramework.h>
#import "Masonry.h"
#import "SDAutoLayout.h"
#import "YYKit.h"
#import "HuangwwFaceAndGestureHeader.h"

@implementation HeaderImageView

- (id)initWithSize:(CGSize)size Vsize:(CGSize)vsize{
    if (self == [super initWithSize:size]) {
        self.headerImageView  = [[UIImageView alloc] init];
        self.headerImageView.contentMode = UIViewContentModeScaleAspectFill;
        [self addSubview:self.headerImageView];
        [self.headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(0));
            make.top.equalTo(@(0));
            make.size.mas_equalTo(size);
        }];
        self.headerImageView.sd_layout
        .leftEqualToView(self)
        .topEqualToView(self)
        .widthIs(size.width)
        .heightIs(size.height);
        [self.headerImageView fuliImageViewRadius];
        
        self.VImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"V_icon"]];
        [self addSubview:self.VImageView];
        [self.VImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.headerImageView.mas_right).equalTo(@(0));
            make.bottom.equalTo(self.headerImageView.mas_bottom).equalTo(@(0));
            make.size.mas_equalTo(vsize);
        }];
    }
    return self;
}

- (void)setUpdateHeaderImage {
    if (self) {
        self.VImageView.hidden = NO;
        [self.headerImageView.layer removeAllSublayers];
        [self.headerImageView FULI_setImageUrlString:@"" placeholderImage:[UIImage imageNamed:@"not_real_name_icon"]];
    }
}

@end

@interface HuangwwFaceAndGestureTool()<HuangwwDrawGestureViewControllerDelegate>

@end

@implementation HuangwwFaceAndGestureTool

static HuangwwFaceAndGestureTool *_sharedManager = nil;

+ (instancetype)defaultManager {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedManager = [HuangwwFaceAndGestureTool new];
    });
    
    return _sharedManager;
}

+ (RACSignal *)FaceRecognition;
{
    return [HuangwwAuthID validateAuthID];
}

+ (void)faceRemoveAuthID {
    [HuangwwAuthID RemoveAuthID];
}

/// 手势识别
+ (void)gestureRecognition
{
    [UIViewController getCurrentVC].navigationController.navigationBar.hidden = YES;
    if ([[PCCircleViewConst getGestureWithKey:gestureFinalSaveKey] length] == 0) {
        [[self checkGesture] subscribeNext:^(id  _Nullable x) {
            
        }];
    }else {
        HuangwwGestureVerifyViewController *gestureVerifyVc = [[HuangwwGestureVerifyViewController alloc] init];
        gestureVerifyVc.isToSetNewGesture = YES;
        UINavigationController *nav = [UIViewController getCurrentVC].navigationController;
        if (nav) {
            
            [[UIViewController getCurrentVC].navigationController pushViewController:gestureVerifyVc animated:YES];
        }else
        {
            [[UIViewController getCurrentVC] presentViewController:gestureVerifyVc animated:YES completion:nil];
        }
    }
}

+ (RACSignal *)checkGesture {
    @weakify(self);
    return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [[FULIAlertTool fuliShowAlertTitle:@"提示" message:@"暂未设置手势密码，是否前往设置？" cancle:@"取消" sure:@"设置"] subscribeNext:^(id  _Nullable x) {
            @strongify(self);
            HuangwwDrawGestureViewController *gestureVc = [[HuangwwDrawGestureViewController alloc] init];
            gestureVc.type = GestureViewControllerTypeSetting;
            gestureVc.delegate = self;
            UINavigationController *nav = [UIViewController getCurrentVC].navigationController;
            if (nav) {
                
                [[UIViewController getCurrentVC].navigationController pushViewController:gestureVc animated:YES];
            }else
            {
                [[UIViewController getCurrentVC] presentViewController:gestureVc animated:YES completion:nil];
            }
            [subscriber sendNext:@""];
            [subscriber sendCompleted];
        } error:^(NSError * _Nullable error) {
            [subscriber sendError:error];
            [subscriber sendCompleted];
        }];
        return [RACDisposable disposableWithBlock:^{
            
        }];
    }];
}

- (void)successSaveGesture {
   
}

- (HeaderImageView *)getUserHeaderImageWithSize:(CGSize)size Vsize:(CGSize)vsize {
    HeaderImageView *headerView = [[HeaderImageView alloc] initWithSize:size Vsize:vsize];
    headerView.VImageView.hidden = NO;
    [headerView.headerImageView fuliAddImageRadiusBorderWithColor:WhiteColor lineWidth:0];

    [headerView.headerImageView FULI_setImageUrlString:@"" placeholderImage:[UIImage imageNamed:@"not_real_name_icon"]];
    return headerView;
}


+ (void)showLoginViewSuccessBlock:(void (^)(void))LoginSuccess {
//    XQCLoginViewController *loginView = [[XQCLoginViewController alloc] init];
//    loginView.loginBlock = ^{
//        LoginSuccess();
//    };
//    UINavigationController *loginNav  = [[UINavigationController alloc] initWithRootViewController:loginView];
//    loginNav.view.frame = CGRectMake(0, kPHONE_HEIGHT, kPHONE_WIDTH, kPHONE_HEIGHT);
//    [loginNav setNavigationBarHidden:YES];
//    [kAppDelegate.window.rootViewController presentViewController:loginNav animated:YES completion:nil];
    [MBProgressHUD showMessage:@"弹出登录界面"];
}

+ (void)hidenLoginView {
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES]; // 收起所有键盘
//    [kAppDelegate.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
    [[UIViewController getCurrentVC] dismissViewControllerAnimated:YES completion:nil];
    
}

+ (BOOL)PhoneTextField:(UITextField *)textField Range:(NSRange)range replacementString:(NSString *)string {
    NSInteger strLength = textField.text.length - range.length + string.length;
    if (string.length > 0) {
        unichar single = [string characterAtIndex:0];//当前输入的字符
        if([textField.text length] == 0)
        {
            if(single != '1')
            {
                
                [textField.text stringByReplacingCharactersInRange:range withString:@""];
                
                return NO;
                
            }
        }
        if (strLength == 2) {
            unichar single2 = [string characterAtIndex:0];//当前第二输入的字符
            if([textField.text length] == 1)
            {
                if((single2 != '3') && (single2 != '4') && (single2 != '5') && (single2 != '7') && (single2 != '8') && (single2 != '9') && (single2 != '6'))
                {
                    
                    [textField.text stringByReplacingCharactersInRange:range withString:@""];
                    
                    return NO;
                    
                }
            }
        }
    }
    return(strLength <= 11);
}

static XQCUnlockViewController *gesture;

+ (void)showUnlockView:(UIWindow *)window {
    if (1) {
        if (kGetUserDefaults(kAuthIDopen) || [kGetUserDefaults(kGestureOpen) boolValue]) {
            gesture = [[XQCUnlockViewController alloc] init];
            gesture.view.frame = CGRectMake(0, kPHONE_HEIGHT, kPHONE_WIDTH, kPHONE_HEIGHT);
            [window.rootViewController.view addSubview:gesture.view];
            [UIView animateWithDuration:0.3 animations:^{
                gesture.view.frame = CGRectMake(0, 0, kPHONE_WIDTH, kPHONE_HEIGHT);
            } completion:^(BOOL finished) {
            }];
        }
    }
}

+ (void)hidenUnlockView {
    [UIView animateWithDuration:0.3 animations:^{
        gesture.view.frame = CGRectMake(0, kPHONE_HEIGHT, kPHONE_WIDTH, kPHONE_HEIGHT);
        gesture = nil;
    } completion:^(BOOL finished) {
    }];
}


+ (BOOL)gestureON {
    return gesture;
}
@end
